package com.example.ribbonconsumer.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author zcx
 * @Title 订单业务层
 * @date 2019年04月01日 9:05
 **/
@Service
public class OrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public String getUserName(String id) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("eureka-client");
        return serviceInstance.getServiceId()+"---"+serviceInstance.getHost()+"----"+serviceInstance.getPort()+"----"+restTemplate.getForEntity("http://eureka-client/username?id={1}",String .class,id).getBody();
    }

    public String helloFallBack(String id) {
        return "哎呀出错了" + id;
    }
}
